package com.fortune.service.impl;

import java.util.List;

import com.fortune.dao.DPostionDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fortune.common.paging.PagingData;
import com.fortune.dao.DRoleDao;
import com.fortune.dto.in.dRole.DRoleAddDto;
import com.fortune.dto.in.dRole.DRoleEditDto;
import com.fortune.dto.in.dRole.DRoleListDto;
import com.fortune.entity.DPosition;
import com.fortune.exception.db.InsertInnerErrorException;
import com.fortune.exception.db.UpdateInnerErrorException;
import com.fortune.service.DRoleService;
import com.fortune.util.UUIDUtil;
import com.fortune.vo.dRole.DRoleB;
import com.fortune.vo.dRole.DRoleDetail;
import com.fortune.vo.dRole.PermissionInDRole;
@Service
public class DRoleServiceImpl implements DRoleService {
	@Autowired
	private DRoleDao dRoleDao;

	@Autowired
	private DPostionDao dPostionDao;

	@Override
	public PagingData<DRoleB> listRoles(DRoleListDto listDto, String dId) {
		List<DRoleB> roles = dRoleDao.listRoles(listDto, dId);
		int count = dRoleDao.countRoles(listDto, dId);
		PagingData<DRoleB> data = new PagingData<>();
		data.setData(roles);
		data.setRecordsTotal(count);
		return data;
	}

	@Override
	public DRoleDetail getRoleDetail(String dId, String roleId) {
		DPosition dPosition = dRoleDao.getPositionByMIdAndDIdAndRoleId(dId, roleId);
		if (dPosition==null) {
			return null;
		}
		DRoleDetail dRoleDetail = new DRoleDetail();
		dRoleDetail.setId(dPosition.getId());
		dRoleDetail.setName(dPosition.getName());
		dRoleDetail.setMemo(dPosition.getMemo());
		dRoleDetail.setType(dPosition.getdRoleType());
		List<PermissionInDRole> permissions = dRoleDao.listPermissions();
		List<String> permissionIds = dRoleDao.getPermissionIdsByRole(dId, roleId);
		for (PermissionInDRole first : permissions) {
			first.setSelected(checkSelected(permissionIds, first));
			if (first.getSubPermissions()!=null&&first.getSubPermissions().size()>0) {
				for (PermissionInDRole second : first.getSubPermissions()) {
					second.setSelected(checkSelected(permissionIds, second));
					if (second.getSubPermissions()!=null&&second.getSubPermissions().size()>0) {
						for (PermissionInDRole function : second.getSubPermissions()) {
							function.setSelected(checkSelected(permissionIds, function));
						}
					}
				}
			}
		}
		dRoleDetail.setPermissions(permissions);
		return dRoleDetail;
	}
	
	private boolean checkSelected(List<String> permissionIds,PermissionInDRole permission) {
		if (permission==null||permission.getId()==null) {
			return false;
		}
		for (String permissionId : permissionIds) {
			if (permission.getId().equals(permissionId)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public DPosition getPositionByMIdAndDIdAndName(String name, String dId) {
		return dRoleDao.getPositionByMIdAndDIdAndName(name, dId);
	}

	@Override
	@Transactional
	public int insertRoleWithPermissions(DRoleAddDto addDto, String dId) {
		String roleId = UUIDUtil.get32UUID();
		int result = dRoleDao.insertRole(addDto,dId,roleId);
		if (result!=1) {
			throw new InsertInnerErrorException("保存岗位失败");
		}
		int exsistPermissionCount = dRoleDao.countExsistPermissionCount(addDto.getPermissions());
		if (exsistPermissionCount!=addDto.getPermissions().size()) {
			throw new InsertInnerErrorException("不能给岗位赋予不存在的权限或重复的权限");
		}
		result = dRoleDao.insertRolePermissions(addDto.getPermissions(),dId,roleId);
		if (result!=addDto.getPermissions().size()) {
			throw new InsertInnerErrorException("保存岗位失败");
		}
		return result;
	}

	@Override
	public DPosition getPositionByMIdAndDIdAndRoleId(String dId, String roleId) {
		return dRoleDao.getPositionByMIdAndDIdAndRoleId(dId,roleId);
	}

	@Override
	public int findEvery(String did, String roleId) {
		return dRoleDao.findEvery(did,roleId);
	}

	@Override
	public DPosition getPositionByMIdAndDIdAndNameAndRoleId(String name, String dId, String roleId) {
		return dRoleDao.getPositionByMIdAndDIdAndNameAndRoleId(name,dId,roleId);
	}

	@Override
	public int updateRoleWithPermissions(DRoleEditDto editDto, String dId) {
		int result = dRoleDao.updateRole(editDto,dId);
		if (result!=1) {
			throw new UpdateInnerErrorException("编辑岗位失败");
		}
		int exsistPermissionCount = dRoleDao.countExsistPermissionCount(editDto.getPermissions());
		if (exsistPermissionCount!= editDto.getPermissions().size()) {
			throw new InsertInnerErrorException("不能给岗位赋予不存在的权限或重复的权限");
		}
		
		dRoleDao.deletePermissionsByRoleId(editDto.getRoleId(),dId);
		result = dRoleDao.insertRolePermissions(editDto.getPermissions(),dId,editDto.getRoleId());
		if (result!= editDto.getPermissions().size()) {
			throw new InsertInnerErrorException("保存岗位失败");
		}
		return result;
	}

	@Override
	public int deleteRole(String dId, String roleId) {
		return dRoleDao.deleteRole(dId,roleId);
	}

	@Override
	public int queryRoleExist(String dId, String roleId) {
		return dPostionDao.queryRoleExist(dId,roleId);
	}

}
